var sysMenu = {
	init : function() {
		
		sysMenu.sysMenuTreegrid=$('#sysMenuTreegrid');
		sysMenu.sysMenuAddDialog=$('#sysMenuAddDialog');
		sysMenu.sysMenuAddFrom=$('#sysMenuAddFrom');
		sysMenu.sysMenuUpdateDialog=$('#sysMenuUpdateDialog');
		sysMenu.sysMenuUpdateFrom=$('#sysMenuUpdateFrom');
		sysMenu.text=$('#text');
		sysMenu.updateText=$('#updateText');
		
		sysMenu.sysMenuTreegrid.treegrid({
		    url: window.ctx +'/SysMenuServlet?command=listSysMenu',
		    idField:'id',
		    treeField:'text',
		    fixed: true,
		    animate:true,
		    fitColumns: true,
		    rownumbers: true,
		    singleSelect: true,
		    toolbar: '#sysMenuToolbar',
		    columns:[[
				{field:'id', checkbox: true}, 
				{field:'text', title:'菜单名称', width: 100},
				{field:'url', title:'菜单地址', width: 100},
				{field:'sort', title:'菜单顺序', width: 30},
				{field:'operateTime', title:'操作时间', width: 100},
				{field:'user__name', title:'操作人', width: 100}
		    ]],
		    onLoadSuccess: function(node, data) {
				$(this).treegrid('expandAll')
			},
			onDblClickCell:function(rowIndex, rowData){
				sysMenu.toolbar.showUpdateDialog(rowData.id);
			}
		});
	},
	toolbar:{
		showAddDialog: function(id) {
			var getFatherMenu = function(v) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=getFatherMenu', {id : v}, function(data) {
					if (data) {
						sysMenu.sysMenuAddFrom.form('load', {
							id: data.id,
							ptext: data.text
						});
						sysMenu.sysMenuAddDialog.dialog('open');
						sysMenu.text.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if(id){
				getFatherMenu(id);
			}else{
				var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
				if(row.length==1){
					getFatherMenu(row[0].id);
				}else{
					getFatherMenu(0);
				}
				
			}
		},
		
		hiddenAddDialog: function() {
			sysMenu.sysMenuAddDialog.dialog('close');
			sysMenu.toolbar.resetAddForm();
		},
		resetAddFrom: function() {
			sysMenu.sysMenuAddFrom.form('reset');
		},
		add:function () {
			if(sysMenu.sysMenuAddFrom.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=addSysMenu', sysMenu.sysMenuAddFrom.serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '新增成功！',
							timeout: 3000,
							showType: 'slide'
						});
						sysMenu.toolbar.resetAddFrom();
						sysMenu.sysMenuTreegrid.treegrid('reload');	
					} else {
						$.messager.alert('系统提示', '新增失败！', 'error')
					}
					$.messager.progress('close');
					sysMenu.toolbar.hiddenAddDialog();
				});
			}
		},
		showUpdateDialog:function(id){
			var getSysMenu = function(v) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=getSysMenu', {id : v}, function(data) {
					if (data) {
						sysMenu.sysMenuUpdateFrom.form('load', {
							id: data.id,
							ptext: data.p__text,
							text:data.text,
							url:data.url,
							sort:data.sort,
							state:data.state,
							memo:data.memo
						});
						sysMenu.sysMenuUpdateDialog.dialog('open');
						sysMenu.updateText.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if(id){
				getSysMenu(id);
			}else{
				var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
				if(row.length==1){
					getSysMenu(row[0].id);
				}else{
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
				
			}
		},
		hiddenUpdateDialog: function() {
			sysMenu.sysMenuUpdateDialog.dialog('close');
			sysMenu.toolbar.resetUpdateForm();
		},
		resetUpdateForm: function() {
			sysMenu.sysMenuUpdateFrom.form('reset');
		},
		
		update:function () {
			if(sysMenu.sysMenuUpdateFrom.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=updateSysMenu', sysMenu.sysMenuUpdateFrom.serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						sysMenu.toolbar.resetUpdateForm();
						sysMenu.sysMenuTreegrid.treegrid('reload');		
					} else {
						$.messager.alert('系统提示', '修改失败！', 'error')
					}
					$.messager.progress('close');
					sysMenu.toolbar.hiddenUpdateDialog();
				});
			}
		},
		
		updateState:function(id){
			var getupdateState = function(v) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=getupdateState', {id : v}, function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						sysMenu.sysMenuTreegrid.treegrid('reload');	
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				});
			}
			if(id){
				getupdateState(id);
			}else{
				var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
				if(row.length==1){
					getupdateState(row[0].id);
				}else{
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据！',
						timeout: 3000,
						showType: 'slide'
					});
				}
				
			}
		},
		redo: function() {
			sysMenu.sysMenuTreegrid.datagrid('unselectAll');
		}
		
	}
}

$(function() {
	sysMenu.init();
});